package com.bootdo.analyze.service.impl;

import com.bootdo.analyze.dao.GoodsSellAIDao;
import com.bootdo.analyze.domain.GoodsSellAIDO;
import com.bootdo.analyze.service.GoodsSellAIService;
import com.bootdo.analyze.util.Dto;
import com.bootdo.analyze.util.DtoUtil;
import com.bootdo.analyze.util.WeiDuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class GoodsSellAIServiceImpl implements GoodsSellAIService {
    @Autowired
    private GoodsSellAIDao goodsSellAIDao;

    @Override
    public List<GoodsSellAIDO> list(Map<String, Object> map) {
        List<GoodsSellAIDO> goodsSellAIDOList = goodsSellAIDao.list(map);
        for (GoodsSellAIDO goodsSellAIDO:goodsSellAIDOList){
            int id = goodsSellAIDO.getId();
            String str_salescount=goodsSellAIDao.getSalescount(id);
            if (str_salescount==null || str_salescount==""){
                str_salescount="0";
            }
            goodsSellAIDO.setSalescount(Integer.parseInt(str_salescount));
            Integer salesbaseint=goodsSellAIDao.getSalesbase(id);
            if (salesbaseint==null){
                salesbaseint=0;
            }
            goodsSellAIDO.setSalesbase(salesbaseint/3);
            Double salespercentage=(double)((goodsSellAIDO.getSalescount())*100/ (goodsSellAIDO.getSalesbase()));
            goodsSellAIDO.setSalespercentage(salespercentage);
            goodsSellAIDO.setTotalPrices(goodsSellAIDao.getTotalPrices(id));

            goodsSellAIDO.setWeiDuUtilList(goodsSellAIDao.getGoodsInfo(id));

        }
        return goodsSellAIDOList;
    }

    @Override
    public int count(Map<String, Object> map) {
        return goodsSellAIDao.count(map);
    }

    @Override
    public List<GoodsSellAIDO> getGoodsFluctuate(Map<String,Object> map) {
        List<GoodsSellAIDO> goodsSellAIDOList = goodsSellAIDao.getGoodsFluctuate(map);
        System.out.println("--------------------ddd----"+goodsSellAIDOList.size());
        for (GoodsSellAIDO goodsSellAIDO:goodsSellAIDOList){
            int id = goodsSellAIDO.getId();
            String str_salescount=goodsSellAIDao.getSalescount(id);
            if (str_salescount==null || str_salescount==""){
                str_salescount="0";
            }
            goodsSellAIDO.setSalescount(Integer.parseInt(str_salescount));

            goodsSellAIDO.setTotalPrices(goodsSellAIDao.getTotalPrices(id));

            List<WeiDuUtil> weiDuUtilList= goodsSellAIDO.getWeiDuUtilList();

            List<WeiDuUtil> weiDuUtils = new ArrayList<WeiDuUtil>();

            Integer bdint = 0;
            for (WeiDuUtil weiDuUtil:weiDuUtilList){
                String b = weiDuUtil.getBusinessDay();
                String bd = b.substring(b.length()-2);
                bdint = Integer.parseInt(bd);
                weiDuUtil.setBusinessDay(bdint.toString());
                for (int i =1 ; i<=31 ;i++){
                    WeiDuUtil weiDuUtil1 = new WeiDuUtil();
                    weiDuUtil1.setCountWD(0);
                    weiDuUtil1.setPricesWD(0.00);
                    weiDuUtil1.setBusinessDay(Integer.toString(i));
                    if (i==bdint){
                        weiDuUtils.add(weiDuUtil);
                        System.out.println(weiDuUtils.get(i-1).getCountWD());
                    }else {
                        weiDuUtils.add(weiDuUtil1);
                    }

                }
            }

            goodsSellAIDO.setWeiDuUtilList(weiDuUtils);

        }
        return goodsSellAIDOList;


    }


    public List<Dto> getGoodsFluctuate1(Map<String,Object> map) {
        List<Dto> dtoList = new ArrayList<Dto>();

        List<GoodsSellAIDO> goodsSellAIDOList = goodsSellAIDao.list(map);
        for (GoodsSellAIDO goodsSellAIDO:goodsSellAIDOList){
            int id = goodsSellAIDO.getId();
            String str_salescount=goodsSellAIDao.getSalescount(id);
            if (str_salescount==null || str_salescount==""){
                str_salescount="0";
            }
            goodsSellAIDO.setSalescount(Integer.parseInt(str_salescount));
            goodsSellAIDO.setSalesbase((goodsSellAIDao.getSalesbase(id)/3));
            Double salespercentage=(double)((goodsSellAIDO.getSalescount())*100/ (goodsSellAIDO.getSalesbase()));
            goodsSellAIDO.setSalespercentage(salespercentage);
            goodsSellAIDO.setTotalPrices(goodsSellAIDao.getTotalPrices(id));
            List<WeiDuUtil> weiDuUtilList = goodsSellAIDao.getGoodsInfo(id);
            for (WeiDuUtil weiDuUtil :weiDuUtilList){
                System.out.println("----------------------"+weiDuUtil.getCountWD());
            }
            /*for (int i = 0;i<weiDuUtilList.size();i++){
                mapResult.put("countWD"+i,weiDuUtilList.get(i).getCountWD());
                mapResult.put("pricesWD"+i,weiDuUtilList.get(i).getPricesWD());
                mapResult.put("goodsSellAIDO"+i,goodsSellAIDO);
            }*/
            Dto dto = DtoUtil.returnSuccess(goodsSellAIDO,weiDuUtilList);
            dtoList.add(dto);
        }
        return dtoList;
    }

    @Override
    public List<GoodsSellAIDO> getStockFluctuate(Map<String, Object> map) {
        List<GoodsSellAIDO> goodsSellAIDOList = goodsSellAIDao.getStockFluctuate(map);
        for (GoodsSellAIDO goodsSellAIDO:goodsSellAIDOList){
            int id = goodsSellAIDO.getId();
            String str_salescount=goodsSellAIDao.getStockcount(id);
            if (str_salescount==null || str_salescount==""){
                str_salescount="0";
            }
            goodsSellAIDO.setSalescount(Integer.parseInt(str_salescount));

            goodsSellAIDO.setTotalPrices(goodsSellAIDao.getStockTotalPrices(id));

            List<WeiDuUtil> weiDuUtilList= goodsSellAIDO.getWeiDuUtilList();

            List<WeiDuUtil> weiDuUtils = new ArrayList<WeiDuUtil>();

            Integer bdint = 0;
            for (WeiDuUtil weiDuUtil:weiDuUtilList){
                String b = weiDuUtil.getBusinessDay();
                String bd = b.substring(b.length()-2);
                bdint = Integer.parseInt(bd);
                weiDuUtil.setBusinessDay(bdint.toString());
                for (int i =1 ; i<=31 ;i++){
                    WeiDuUtil weiDuUtil1 = new WeiDuUtil();
                    weiDuUtil1.setCountWD(0);
                    weiDuUtil1.setPricesWD(0.00);
                    weiDuUtil1.setBusinessDay(Integer.toString(i));
                    if (i==bdint){
                        weiDuUtils.add(weiDuUtil);
                        System.out.println(weiDuUtils.get(i-1).getCountWD());
                    }else {
                        weiDuUtils.add(weiDuUtil1);
                    }

                }
            }

            goodsSellAIDO.setWeiDuUtilList(weiDuUtils);


        }
        return goodsSellAIDOList;
    }

    @Override
    public List<WeiDuUtil> getStockInfo(Map<String, Object> map) {
        List<WeiDuUtil> weiDuUtilList = goodsSellAIDao.getStockInfo(map);
        List<WeiDuUtil> weiDuUtils = new ArrayList<WeiDuUtil>();

        Integer bdint = 0;
        for (WeiDuUtil weiDuUtil:weiDuUtilList){
            System.out.println("weiDuUtil-----------------"+weiDuUtil.getCountWD());
            String b = weiDuUtil.getBusinessDay();
            String bd = b.substring(b.length()-2);
            bdint = Integer.parseInt(bd);
            weiDuUtil.setBusinessDay(bdint.toString());
            System.out.println(weiDuUtil.getBusinessDay()+"---------------------------day");
            for (int i =1 ; i<=12 ;i++){
                WeiDuUtil weiDuUtil1 = new WeiDuUtil();
                weiDuUtil1.setCountWD(0);
                weiDuUtil1.setPricesWD(0.00);
                weiDuUtil1.setBusinessDay(Integer.toString(i));
                if (i==bdint){
                    weiDuUtils.add(weiDuUtil);
                    System.out.println(weiDuUtils.get(i-1).getCountWD());
                }else {
                    weiDuUtils.add(weiDuUtil1);
                }

            }
        }
        return weiDuUtils;

    }

    @Override
    public List<GoodsSellAIDO> getInventoryBacklog(Map<String, Object> map) {
        return goodsSellAIDao.getInventoryBacklog(map);
    }

    @Override
    public List<GoodsSellAIDO> getInventoryTurnover(Map<String, Object> map) {
        return goodsSellAIDao.getInventoryTurnover(map);
    }

    @Override
    public List<WeiDuUtil> getGoodsInfo11(Map<String, Object> map) {
        List<WeiDuUtil> weiDuUtilList = goodsSellAIDao.getGoodsInfo11(map);
        List<WeiDuUtil> weiDuUtils = new ArrayList<WeiDuUtil>();

        Integer bdint = 0;
        for (WeiDuUtil weiDuUtil:weiDuUtilList){
            System.out.println("weiDuUtil-----------------"+weiDuUtil.getCountWD());
            String b = weiDuUtil.getBusinessDay();
            String bd = b.substring(b.length()-2);
            bdint = Integer.parseInt(bd);
            weiDuUtil.setBusinessDay(bdint.toString());
            System.out.println(weiDuUtil.getBusinessDay()+"---------------------------day");
            for (int i =1 ; i<=12 ;i++){
                WeiDuUtil weiDuUtil1 = new WeiDuUtil();
                weiDuUtil1.setCountWD(0);
                weiDuUtil1.setPricesWD(0.00);
                weiDuUtil1.setBusinessDay(Integer.toString(i));
                if (i==bdint){
                    weiDuUtils.add(weiDuUtil);
                    System.out.println(weiDuUtils.get(i-1).getCountWD());
                }else {
                    weiDuUtils.add(weiDuUtil1);
                }

            }
        }
        return weiDuUtils;

    }

    @Override
    public List<WeiDuUtil> getGoodsInfo(int id) {
        return goodsSellAIDao.getGoodsInfo(id);
    }


    @Override
    public List<GoodsSellAIDO> goodlist(Map<String, Object> map) {
        List<GoodsSellAIDO> goodsSellAIDOList = goodsSellAIDao.list(map);
        for (GoodsSellAIDO goodsSellAIDO:goodsSellAIDOList){
            int id = goodsSellAIDO.getId();
            String str_salescount=goodsSellAIDao.getSalescount(id);
            if (str_salescount==null || str_salescount==""){
                str_salescount="0";
            }
            goodsSellAIDO.setSalescount(Integer.parseInt(str_salescount));
            goodsSellAIDO.setTotalPrices(goodsSellAIDao.getTotalPrices(id));
        }
        return goodsSellAIDOList;
    }

    @Override
    public List<GoodsSellAIDO> getGoodsPurchase(Map<String, Object> map) {
        return goodsSellAIDao.getGoodsPurchase(map);
    }

    @Override
    public List<GoodsSellAIDO> getNetContrast(Map<String, Object> map) {
        return goodsSellAIDao.getNetContrast(map);
    }
}
